import 'result_model.dart';

///分页查询请求
class PagingQueryReq {
  int? index, size;

  PagingQueryReq({this.index = 0, this.size = 10});

  Map<String, dynamic> toMap() {
    return {
      "index": index ?? 0,
      "size": size ?? 10,
    };
  }
}

///行转对象回调处理
typedef RowToClassCallback<T> = T Function(Map<String, dynamic> row);

///解析到数据行
List<T> parseToRows<T>(List<dynamic> rows, RowToClassCallback<T> handler) {
  if (rows.isNotEmpty) {
    return rows.map((row) => handler(row)).toList();
  }
  return [];
}

///分页查询响应
class PagingQueryRes<T> extends BaseModelRes {
  final int total;
  final List<T> rows;

  PagingQueryRes({
    required super.retCode,
    required super.retMessage,
    this.total = 0,
    this.rows = const [],
  });

  factory PagingQueryRes.fromModel(
    final ResultModelRes res,
    final RowToClassCallback<T> rowHandler,
  ) {
    final map = res.toMap;
    return PagingQueryRes(
      retCode: res.retCode,
      retMessage: res.retMessage,
      total: (map['total'] ?? 0),
      rows: parseToRows(map['rows'] ?? [], rowHandler),
    );
  }
}
